<?php

namespace App\Admin\Controllers;

use App\Models\WechatUser;
use Encore\Admin\Controllers\AdminController;
use App\Models\WechatMenu;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Tree;
use Encore\Admin\Layout\Content;

class WechatMenuController extends AdminController
{
    public function index(Content $content)
    {
        $tree = new Tree(new WechatMenu());
        return $content
            ->header('微信菜单')
            ->body($tree);
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(WechatMenu::findOrFail($id));

        $show->field('id', __('ID'));
        $show->field('title', __('菜单名称'));
        $show->field('sort', __('排序'));
        $show->field('type', __('消息类型'));
        $show->field('content', __('消息内容'));
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new WechatMenu());
        $form->display('id', __('ID'));
        $form->display('pid', __('父级ID'))->customFormat(function ($value) {
            if ($value == 0) {
                return "顶级菜单";
            }
            return WechatMenu::query()->where('id', $value)->value('title');
        })->disable();
        $form->text('title', __('菜单名称'));
        $form->select('type', __('消息类型'))->options(function ($type) {
            if ($this->pid == 0) {
                return [1 => '发送消息', 2 => '跳转网页'];
            }
            return [1 => '发送消息', 2 => '跳转网页'];
        })->default('type')->when(1, function (Form $form) {
            $form->textarea('content', __('菜单内容'));
        })->when(2, function (Form $form) {
            $form->text('content', __('网页链接'));
        });
        $states = [
            'on'  => ['value' => 1, 'text' => '正常', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
        ];
        $form->switch('state', __('状态'))->options($states);
        $form->text('sort', __('排序'));
        $form->footer(function ($footer) {

            // 去掉`重置`按钮
            $footer->disableReset();

            // 去掉`提交`按钮
//            $footer->disableSubmit();

            // 去掉`查看`checkbox
            $footer->disableViewCheck();

            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();

            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();

        });

        return $form;
    }
}
